package com.hc.tenant.config;

import org.eclipse.paho.client.mqttv3.*;

public class MqttPushClient {

    private MqttClient mqttClient;

    public MqttPushClient(MqttClient mqttClient) {
        this.mqttClient = mqttClient;
    }

    public void publish(String TopicName, String message ) throws MqttException {
        publish(0, false, TopicName, message );

    }


    public void publish(int qos, boolean retained, String topicName, String message) throws MqttException {
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setQos(0);
        // 持久化设置
        mqttMessage.setRetained(retained);
        mqttMessage.setPayload(message.getBytes());
        mqttClient.publish(topicName, mqttMessage);
    }

    public void subscribe(String topic) {
        subscribe(topic, 0,null);
    }

    public void subscribe(String topic, int qos, IMqttMessageListener listener) {
        try {
            mqttClient.subscribe(topic, qos, listener);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
    /**
     * 空方法，用于nacos 定时刷新对象
     * */
    public void refresh(){
    }


}